*merge tax data

use "{hmrc_datadir}\CBT\tax_ISO.dta" , clear
keep if year>1999&year<2012
sort country year

merge 1:1 alpha3code year using "{hmrc_datadir}\Data\CBT\macro.dta"
save "{hmrc_datadir}\CBT\tax_macro.dta"

merge 1:1 alpha3code year using "{hmrc_datadir}\CBT\macr_IMF.dta" 
save "{hmrc_datadir}\CBT\tax_macrIMF.dta"

drop _merge
merge 1:1 alpha3code year using "{hmrc_datadir}\CBT\gov_ind_ready.dta"
drop if _merge==2
drop Ctr _merge
save "{hmrc_datadir}\CBT\tax_macrIMF_governance.dta"

merge 1:1 alpha3code year using "{hmrc_datadir}\CBT\dobus_ISO.dta"
drop if _merge==2
drop _merge
save "{hmrc_datadir}\CBT\tax_macrIMF_governance_db.dta"

merge 1:1 alpha3code year using "{hmrc_datadir}\CBT\FIFM_ind.dta"
drop _merge
save "{hmrc_datadir}\CBT\tax_macrIMF_governance_db_FIFM.dta"

merge 1:1 alpha3code year using "{hmrc_datadir}\CBT\world_bank indicator.dta"
drop if _merge==2
drop _merge
save "{hmrc_datadir}\CBT\cbt1.dta"



/*do "${hmrc_codedir}\merge_iso"*/
use "${hmrc_datadir}\Trade\iso_eutable.dta", clear
append using "${hmrc_datadir}\Trade\iso_matchtable_noneu.dta"
duplicates drop
save "${hmrc_datadir}\Trade\iso_table.dta", replace

use "${hmrc_datadir}\CBT\cbt1", clear
drop country
forvalue i=2000/2011{
preserve
keep if year==`i'
merge 1:m alpha3code using "${hmrc_datadir}\Trade\iso_table.dta"
drop if _merge==2
drop _merge
save "${hmrc_datadir}\CBT\cbt`i'", replace
restore
}

use "${hmrc_datadir}\CBT\cbt2000", clear
forvalue i=2001/2011{
append using "${hmrc_datadir}\CBT\cbt`i'"
}
drop byear FY access_FI access_FM percentile_governance doingbusiness_rank doingbusiness_distancetofront
replace country="United Kingdom" if alpha3code=="GBR"
replace alpha2code ="GB" if alpha3code=="GBR"
compress
save "${hmrc_datadir}\CBT\cbt_ready", replace

use "${hmrc_datadir}\CBT\marginal tax rate", clear
keep year x
rename x uk_tax
merge 1:m year using "${hmrc_datadir}\CBT\cbt_ready"
browse if _merge==1
keep if _merge==3
drop _merge
gen low_tax_country=tax<uk_tax
replace low_tax_country=. if tax==.
tab low_tax year
gen high_tax_country=tax>uk_tax
replace high_tax_country=. if tax==.
tab high_tax_country year
gen low_tax_wedge=uk_tax-tax if low_tax_country==1
gen high_tax_wedge=tax-uk_tax if high_tax_country==1

drop country
duplicates drop
save  "${hmrc_datadir}\CBT\cbt_ready", replace


